package Scuttlebutt;

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import glue.ftp.*;
import javax.swing.event.*;

public class FilePopupMenu extends JPopupMenu
{
	long num_of_bytes = 0; 
	
	public class EventDeleteFile extends JMenuItem implements ActionListener{
		
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public EventDeleteFile(Scuttlebutt parent, int x, int y){
			super("Delete");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0)
		{
			SortableNode o = (SortableNode)parent.getProject().getTree().getLastSelectedPathComponent();
			String path = new String(new String((o.getRemoteDirectory().length() > 0)?new String(o.getRemoteDirectory() + File.separatorChar):"") + o);

			String msg = new String("Are you sure you want to delete \'" + path + "\'. This cannot be undone.");
			if(JOptionPane.showConfirmDialog(parent, msg, "Warning",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
				try{
					parent.getFTP().fileDelete(path);
				}catch(java.io.IOException e){
					System.err.println(e);
				}catch(FtpException f){
					System.err.println("Failed to delete file:" + f);
				}
				
				parent.getEditPane().remove(o.getNodePath());
				
				File f = new File(o.getNodePath());
				f.delete();
				
				
				o.removeFromParent();
				
				parent.updateTree();
			}else{
			}
		}
	}
	
	
	
	
	
	public class DisplayFile extends JMenuItem implements ActionListener{
		
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public DisplayFile(Scuttlebutt parent, int x, int y){
			super("Open");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0)
		{
			SortableNode node = (SortableNode)parent.getProject().getTree().getLastSelectedPathComponent();
	    	if((node != null) && (node.getNodePath() != null)){
		    	File path = new File(node.getNodePath());
		    	if(path != null){
			    	if(path.isFile()){
			   		 	parent.getEditPane().Open(path.getPath());
			   		 	parent.SaveProject();
			    	}
		    	}
		    	
		    	path = null;
	    	}
		}
	}
	
	
	public class UploadFile extends JMenuItem implements ActionListener{
		
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public UploadFile(Scuttlebutt parent, int x, int y){
			super("Upload");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0)
		{
			SortableNode o = (SortableNode)parent.getProject().getTree().getLastSelectedPathComponent();

			String path = new String( o.toString());
			Tab tab = parent.getEditPane().search(o.getNodePath());
			if(tab != null){
				tab.Save();
				tab.setChanged();
			}
			File f = new File(o.getNodePath());
			int length = (int)f.length();
			f = null;
			new Thread(new UploadThread(parent)).start();

		}
		
	}	
	
	class UploadThread implements Runnable{
		Scuttlebutt parent = null;

		public UploadThread(Scuttlebutt parent){
			this.parent = parent;
		}
		public void run(){
			SortableNode o = (SortableNode)parent.getProject().getTree().getLastSelectedPathComponent();
			Download d = new Download(parent, o);
			parent.getFileTransferTable().addDownload(d);
			parent.getFileTransferTable().startDownloading();
		}
	}
	
	
	public FilePopupMenu(Scuttlebutt parent, int x, int y)
	{
		add(new DisplayFile(parent, x, y));
		add(new EventDeleteFile(parent, x, y));
		addSeparator();
		add(new UploadFile(parent, x, y));
		SortableNode o = (SortableNode)parent.getProject().getTree().getLastSelectedPathComponent();
		if((parent.isConnected()) && (!parent.getFileTransferTable().isFileDownloading(o.getNodePath())) || (!o.isUploaded()) ){
			((JMenuItem)getComponent(1)).setEnabled(true);
		}else{
			((JMenuItem)getComponent(1)).setEnabled(false);
		}
		
		if(!parent.isConnected()){
			((JMenuItem)getComponent(3)).setEnabled(false);
		}
		
		
	}	
	

}

